<?php

class Utilitarios_UsuariosController extends Zend_Rest_Controller {

	public function init() {
		/* Initialize action controller here */
		$ajaxContext = $this->_helper->contextSwitch();
		$ajaxContext->setAutoJsonSerialization(true);
		$ajaxContext
		->addActionContext('index', array('json'))
		->addActionContext('get', array('json'))
		->addActionContext('post', array('json'))
		->addActionContext('put', array('json'))
		->addActionContext('delete', array('json'))
		->initContext('json');
	}

	public function indexAction(){
		$model_funcao=new Application_Model_Utilitarios_Usuario();
		$filter=$this->getRequest()->getParam('filter');
		$node=$this->getRequest()->getParam('node');
		$this->view->node=$node;
		if($node!=""){
			$filter['primario']=$node;
		}

		$funcao=$model_funcao->index($filter, $this->getRequest()->getParam('start'), $this->getRequest()->getParam('limit'), $this->getRequest()->getParam('sort'));

		if($node!=""){
			foreach($funcao['data'] as &$a0){
				$a0["expanded"]=true;
				$a1=$model_funcao->index(array('primario'=>$a0['id']));
				foreach($a1['data'] as &$a2){
					$a2["expanded"]=true;
					$a3=$model_funcao->index(array('primario'=>$a2['id']));
					$a2['data']=$a3['data'];
				}
				$a0['data']=$a1['data'];
			}
		}

		$this->view->success=$funcao['success'];
		$this->view->mensagem=$funcao['message'];
		$this->view->data=$funcao['data'];
	}

	public function getAction(){
	}

	public function postAction(){
		$post = Zend_Json::decode($this->getRequest()->getRawBody());

		$model_funcao=new Application_Model_Utilitarios_Funcao();
		$funcao=$model_funcao->post($post);

		$this->view->success=$funcao['success'];
		$this->view->mensagem=$funcao['message'];
		$this->view->data=$funcao['data'];
	}

	public function putAction(){
		$id = $this->getRequest()->getParam('id');
		$post = Zend_Json::decode($this->getRequest()->getRawBody());

		$model_funcao=new Application_Model_Utilitarios_Funcao();
		$funcao=$model_funcao->put($id,$post);

		$this->view->success=$funcao['success'];
		$this->view->mensagem=$funcao['message'];
		$this->view->data=$funcao['data'];
	}

	public function deleteAction(){
		$id = $this->getRequest()->getParam('id');

		$model_funcao=new Application_Model_Utilitarios_Funcao();
		$funcao=$model_funcao->delete($id);

		$this->view->success=$funcao['success'];
		$this->view->mensagem=$funcao['message'];
		$this->view->data=$funcao['data'];
	}

}